﻿using System.Windows.Input;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions;
using Bookvar.UI.Commands;

namespace Bookvar.UI.ViewModel
{
    internal partial class MindmapViewModel
    {
        private class AddNewTopicCommand : CommandModel
        {
            private readonly MindmapViewModel viewModel;

            public AddNewTopicCommand( MindmapViewModel viewModel )
                : base( BookvarCommands.AddNewTopic )
            {
                this.viewModel = viewModel;
            }

            protected internal override void OnExecute( object sender, ExecutedRoutedEventArgs e )
            {
                Topic newTopic = new Topic();
                AddTopicAction action;

                if ( viewModel.SelectedTopic != null && viewModel.SelectedTopic.Parent != null )
                {
                    action = new AddTopicAction( newTopic );
                    action.Parent = viewModel.SelectedTopic.Parent;
                    action.InsertIndex = viewModel.SelectedTopic.Parent.SubTopics.IndexOf( viewModel.SelectedTopic ) + 1;
                }
                else
                {
                    action = new AddTopicAction( newTopic ) { Parent = viewModel.Mindmap.MainTopic };
                }

                ActionService.Execute( action );
            }

            protected internal override void OnCanExecute( object sender, CanExecuteRoutedEventArgs e )
            {
                e.CanExecute = true;
                e.Handled = true;
            }
        }
    }
}